#ifndef RESULT_H
#define RESULT_H

#include <QVariant>

template<typename T>
class ResultItem {
public:
//    virtual T fromMap(QVariantMap &map) = 0;
    static QList<T> fromList(QVariantList &list) {
        QList<T> items;
        for (int i = 0; i < list.count(); ++i) {
            auto v = list.at(i).toMap();
            items << T::fromMap(v);
        }
        return items;
    }
protected:
    int m_code;
    QVariant m_data;
};

template<typename T>
class Result {

protected:
    int m_code;
    QVariant m_data;
};

#define mv(v) m_##v
template<typename T, typename F>
class ResultBy {
public:
    QList<F> values() {
        return m_values;
    }
protected:
    int m_code;
    QVariant m_data;
    QList<F> m_values;
};

#endif // RESULT_H


/**
 * Result<T> 默认提供 code/msg/data
 * 
 * ResultItem<T> 默认提供 fromList 遍历调用自身 fromMap 构造 T 实体列表
 * 
 * ResultBy<T,F> 实体下的 F 实体数据封装
 *               实体中包含结构集时可向下提供默认的 F 实体列表数据
 * 
 * 
 * 这个例子中，当使用 ResultBy 时，将自动为 T 添加一个 values() 函数用于获取 QList<F>
 */

// #include <QVariant>
// class TemporaryResultData {
//     TemporaryResultData() {}
// public:
//     static TemporaryResultData fromMap(QVariantMap &data) {}
//     static QList<TemporaryResultData> fromList(QVariantList &list);
// };

// class TemporaryResult : public ResultBy<TemporaryResult, TemporaryResultData> {
//     TemporaryResult() {}
// public:
//     static TemporaryResult fromResponse(Response &resp) {}
// };


/**
 * 提供一个简单的结果集示例
 * {
 *    "code": 200,
 *    "msg": "成功",
 *    "datas": [
 *        {
 *            "user": "api"
 *            "pass": "1234"
 *        }
 *        {
 *            "user": "req"
 *            "pass": "1234"
 *        }
 *        {
 *            "user": "resp"
 *            "pass": "1234"
 *        }
 *    ]
 * }
 */